﻿#region

using System.ComponentModel.DataAnnotations;
using SqlSugar;
using Vin.Tool.Core.DateTimeCategory;

#endregion

namespace Vin.Extension.Framework.Dto;

public record VinFrameworkBaseListDto
{
    /// <summary>
    /// 页码
    /// </summary>
    public int Page { get; set; } = 1;

    /// <summary>
    /// 每页数量
    /// </summary>
    [Range(0, 50, ErrorMessage = "每页请求数量不可以大于50")]
    public int PageSize { get; set; } = 10;

    /// <summary>
    /// 不分页
    /// </summary>
    public bool? NotPage { get; set; }

    /// <summary>
    /// 开始时间
    /// </summary>
    public DateTime? StartTime { get; set; }

    /// <summary>
    /// 结束时间
    /// </summary>
    public DateTime? EndTime { get; set; }

    /// <summary>
    /// 关键词搜索
    /// </summary>
    public string? Keyword { get; set; }

    /// <summary>
    /// 分页总数
    /// </summary>
    private RefAsync<int>? _refTotal;

    /// <summary>
    /// 分页总数
    /// </summary>
    public RefAsync<int> RefTotal => _refTotal ??= new RefAsync<int>();

    /// <summary>
    /// 分页模型
    /// </summary>
    private PageModel? _pageModel;

    public PageModel PageModel => _pageModel ??= new PageModel()
    {
        PageIndex = Page,
        PageSize = PageSize
    };

    /// <summary>
    /// 总数
    /// </summary>
    public int TotalCount
    {
        get
        {
            if (_pageModel?.TotalCount > 0) return _pageModel.TotalCount;
            if (_refTotal?.Value > 0) return _refTotal.Value;
            return 0;
        }
    }

    public void InitCurrentMonth()
    {
        this.StartTime = this.StartTime ?? DateTime.Now.Date.GetFirstDayOfMonth();
        this.EndTime = this.EndTime ?? DateTime.Now.Date.AddDays(1);
    }
}